package cn.px.rest.controller.sb;

import cn.px.common.bean.AbstractPageResultBean;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.util.R;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import cn.px.dao.dto.sb.SbCheckFormDTO;
import cn.px.dao.entity.sb.SbCheckForm;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.vo.export.sb.ExportSbCheckFormVO;
import cn.px.dao.vo.query.sb.SbCheckFormVO;
import cn.px.rest.log.annotation.SysLog;
import cn.px.service.sb.SbCheckFormService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description 验收表 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sb/sb-check-forms")
public class SbCheckFormController {

  private final  SbCheckFormService inStoreFormService;

  /**
   * 通过id查询单条记录
   *
   * @param id 主键
   * @return R
   */
  @GetMapping("/{id}")
  public R<SbCheckFormVO> getById(@PathVariable("id") String id){
      return new R<>(inStoreFormService.getDetail(id));
  }

  /**
   * 新增记录
   *
   * @param inStoreFormDTO 验收表DTO
   * @return R
   */
  @SysLog("新增验收表")
  @PostMapping
  @PreAuthorize("@pms.hasPermission('store-in-store-forms-add')")
  public R save(@Validated({AddGroup.class}) @RequestBody SbCheckFormDTO inStoreFormDTO) {
      return new R<>(inStoreFormService.saveModelByDTO(inStoreFormDTO));
  }

  /**
   * 修改记录
   *
   * @param inStoreFormDTO 验收表DTO
   * @return R
   */
  @SysLog("修改验收表")
  @PutMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('store-in-store-forms-edit')")
  public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody SbCheckFormDTO inStoreFormDTO) {
      inStoreFormService.updateModelByDTO(inStoreFormDTO);
      return new R<>();
  }

    /**
     * 提交更新库存，或者
     * 生成设备信息，并将设备的配件入库，配件如果没有怎么办？
     *
     * 这个配件的列表应该都已经新建过的，如果没有新建怎么办
     *
     * @param id 验收表DTO
     * @return R
     */
    @SysLog("验收通过")
    @PutMapping("/in/{id}")
    @PreAuthorize("@pms.hasPermission('store-in-store-forms-edit')")
    public R updateStore(@PathVariable("id") String id) {
        inStoreFormService.checkIn(id);
        return new R<>();
    }

  /**
   * 通过id删除一条记录
   *
   * @param id 主键
   * @return R
   */
  @SysLog("删除验收表")
  @DeleteMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('store-in-store-forms-del')")
  public R removeById(@PathVariable String id){
    inStoreFormService.cascadingDeleteByKey(id);
    return new R<>();
  }

    /**
     * 批量记录
     *
     * @param ids 主键
     * @return R
     */
    @SysLog("批量删除验收表")
    @DeleteMapping("")
    @PreAuthorize("@pms.hasPermission('store-in-store-forms-del')")
    public R removeIds(@RequestBody List<String> ids){
        inStoreFormService.batchDelete(ids);
        return new R<>();
    }

  /**
   * 获取分页
   *
   * @param pageNum 当前页码
   * @param pageSize 每页条数
   * @param inStoreFormDTO 验收表DTO
   * @return R
   */
  @GetMapping("/page")
  public R<AbstractPageResultBean<SbCheckFormVO>> query(SbCheckFormDTO inStoreFormDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
      AbstractPageResultBean<SbCheckFormVO> vo = inStoreFormService.selectPageList(inStoreFormDTO, pageNum, pageSize);
      return new R<>(vo);
  }

  /**
   * 获取列表
   *
   * @param inStoreFormDTO 验收表DTO
   * @return R
   */
  @GetMapping("")
  public R query(SbCheckFormDTO inStoreFormDTO) {
      return new R<>(inStoreFormService.getModelListByDTO(inStoreFormDTO));
  }

  /**
     * 验收表导出
     *
     * @param inStoreFormDTO 验收表DTO
     * @return R
     */
  @GetMapping("/export")
  @SysLog("验收表导出")
  @PreAuthorize("@pms.hasPermission('store-in-store-forms-export')")
  public void export(HttpServletResponse response, SbCheckFormDTO inStoreFormDTO) {
    List<SbCheckForm> list = inStoreFormService.getModelListByDTO(inStoreFormDTO);
    ExcelUtil.exportResponseDict(response, ExportSbCheckFormVO.class, BeanConverterUtil.copyListProperties(list, ExportSbCheckFormVO.class), "验收表");
  }

}
